using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;

namespace Domain.Core
{
    /// <summary>
    /// Class that supports the TYPESAFE ENUM pttern.
    /// </summary>
    /// <typeparam name="T">A class that derives from this base class.</typeparam>
    /// <remarks>Code from http://msmvps.com/blogs/jon.skeet/archive/2006/01/05/classenum.aspx.</remarks>
    public abstract class EnumType<T> where T : EnumType<T>
    {
        #region Static Fields

        private static List<T> values = new List<T>();
        private static Dictionary<String, T> nameList = new Dictionary<string, T>();

        #endregion

        #region Static Methods

        protected static ReadOnlyCollection<T> BaseValues()
        {

            return values.AsReadOnly();
        }

        protected static T BaseValueOf(String name)
        {

            return nameList[name];
        }

        #endregion

        #region Non-Public Fields

        private String name;

        #endregion

        #region Properties

        public String Name
        {
            get
            {
                return name;
            }
        }

        #endregion

        #region Constructors

        protected EnumType(String name)
        {

            if (nameList.ContainsKey(name))
            {
                throw new ArgumentException(
                String.Format("Class {0} already contains an element with name \"{1}\".",
                typeof(T).Name, name));
            }

            this.name = name;

            values.Add((T)this);
            nameList.Add(name, (T)this);
        }

        #endregion

        #region Class Base Overrides

        public override string ToString()
        {
            return name;
        }

        #endregion
    } 

}
